package com.ruoyi.web.domain.insect;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ruoyi.common.annotation.Excel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description 设备昆虫识别昆虫信息
 * @author yaojianbo
 * @date 2023-02-23
 */
@Data
@TableName("tt_product_pest_record")
public class InsectRecord  {

    @ApiModelProperty(value = "id")
    @TableId(value = "id",type = IdType.ASSIGN_ID)
    private String id;

    @ApiModelProperty(value = "设备ID")
    @Excel(name = "设备编号",  lineMergeId = true,lineMerge = true , sort = 2)
    private String deviceId;

    @ApiModelProperty(value = "图片ID")
    private Long photoId;

    @ApiModelProperty(value = "昆虫类型")
    private String insectType;

    @ApiModelProperty(value = "昆虫名称")
    @Excel(name = "昆虫名称", sort = 4)
    private String insectName;

    @ApiModelProperty(value = "昆虫编码")
    private String insectUuid;

    @ApiModelProperty(value = "昆虫数量")
    @Excel(name = "昆虫数量", sort = 5)
    private Integer count;

    @ApiModelProperty(value = "昆虫位置")
    private String position;

    @ApiModelProperty(value = "图片日期" )
    private LocalDateTime photoTime;

    @ApiModelProperty(value = "创建人")
    private String crtName;

    @ApiModelProperty(value = "修改人")
    private String mdfName;

    @ApiModelProperty(value = "数据时间（统计报表用）")
    @TableField(exist = false)
    @Excel(name = "图片日期",lineMerge = true, lineMergeId = true, sort = 3)
    private String dataTime;

    @Excel(name = "序号",  sort = 1)
    @TableField(exist = false)
    private Integer no;

    //@TableLogic //逻辑删除
    @JsonIgnore
    private int delFlag;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime  crtTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime mdfTime;


    public static void buildNo(List<InsectRecord> records){
        for (int i = 0; i < records.size(); i++) {
             records.get(i).setNo(i + 1);
        }
    }


    public static InsectRecord of(String category, List<JSONObject> v, String deviceId, Long picId, String insectUuid) {
        InsectRecord insectRecord = new InsectRecord();
        insectRecord.setDeviceId(deviceId);
        insectRecord.setPhotoId(picId);
        insectRecord.setInsectName(category);
        insectRecord.setInsectUuid(insectUuid);
        insectRecord.setInsectType(category);
        insectRecord.setCount(v.size());
        insectRecord.setPosition(getPositionFormat(v)); //设置昆虫识别的结果
        insectRecord.setCrtTime(LocalDateTime.now());
        insectRecord.setMdfTime(LocalDateTime.now());
        return insectRecord;
    }

    public static String getPositionFormat(List<JSONObject> pestList){
        List<String> buffer = new ArrayList<>();

        for(int i = 0; i < pestList.size(); i++){
            JSONObject location = pestList.get(i).getJSONObject("location");
            Double score = pestList.get(i).getDouble("score"); //置信度
            String postion = String.format("{\"x\": %s,\"y\": %s ,\"w\": %s ,\"h\": %s,\"s\": %.2f}",
                    location.getString("left"),location.getString("top"),location.getString("width"),location.getString("height"),score);
            buffer.add(postion);
        }

        String join = buffer.stream().collect(Collectors.joining("&"));
        return join;
    }
}
